package com.aaa.marketresearch.android.app;

import android.app.Activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import android.widget.ListView;
import com.aaa.marketresearch.android.data.UserInfo;
import com.aaa.marketresearch.android.data.domain.BillContent;

import com.aaa.marketresearch.android.service.HttpClient;
import com.aaa.marketresearch.android.util.SimpleLog;

import com.aaa.marketresearch.android.util.connector.IDataTextResponse;
import com.aaa.marketresearch.android.widget.BillContentAdapter;
import org.json.simple.JSONObject;

import java.util.List;

/**
 * @author Nail
 */
public class BillScreen extends Activity {

    private ListView listView;

    private List<BillContent> billContentList = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill_screen);

        this.listView = (ListView) findViewById(R.id.billContentList);
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.updateBillContent();
    }

    public void saveBillButton(View view) {
        SimpleLog.info("saveBillButton");

        JSONObject jBill = UserInfo.getInstance().getJSONBill();

        HttpClient httpClient = new HttpClient(this.getApplicationContext());
        httpClient.saveBill(jBill, new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                SimpleLog.info("saved");
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(BillScreen.this);
                alertDialog.setMessage("Чек сохранен");
                alertDialog.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                UserInfo.getInstance().clear();
                                Intent intent = new Intent(BillScreen.this, MainMenu.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                BillScreen.this.startActivity(intent);
                            }
                        }
                );
                alertDialog.show();
            }

            @Override
            public void onError() {

            }
        });
    }

    public void addOfferButton(View view) {
        SimpleLog.info("addOfferButton");
        BillContent billContent = UserInfo.getInstance().getCurrentBillContent().makeSame();
        UserInfo.getInstance().setCurrentBillContent(billContent);
        Intent intent = new Intent(this, OfferScreen.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        this.startActivity(intent);
    }


    public void updateBillContent() {
        this.billContentList = UserInfo.getInstance().getBill();
        BillContentAdapter adapter = new BillContentAdapter(this, this.billContentList);
        this.listView.setAdapter(adapter);
    }
}